
<?php
$sidebar =
        '<ul>
    <li><a href=\"' . site_url("records/$pool/men") . '\">Mehed</a></li>
    <li><a href=\"' . site_url("records/$pool/m-juniors") . '\">Juuniorid</a></li>
    <li><a href=\"' . site_url("records/$pool/m-youth") . '\">Noored</a></li>
    <li><a href=\"' . site_url("records/$pool/boys") . '\">Poisid</a></li>
    <li class=\"divider\"></li>
    <li><a href=\"' . site_url("records/$pool/women") . '\">Naised</a></li>
    <li><a href=\"' . site_url("records/$pool/w-juniors") . '\">Juuniorid</a></li>
    <li><a href=\"' . site_url("records/$pool/girls") . '\">Tüdrukud</a></li>
</ul>';

$sidebar = str_replace("\n", '\n', $sidebar);
$buttons = '<div class=\"btn-group\" data-toggle=\"buttons-radio\">
  <button id=\"50\" class=\"btn btn-large btn-primary\" data-toggle=\"button\">50 m</button>
  <button id=\"25\" class=\"btn btn-large\">25 m</button>
</div>';

$buttons = str_replace("\n", '\n', $buttons);
?> 

<style>
    .btn-group {
        float: right;
        margin-top: -50px;
    }
</style>
<script>
    var category_code = "<?php echo $category_code; ?>";
    var records_sidebar = "<?php echo $sidebar; ?>";
    $(".page-header").html("<h1>Tartu UK rekordid</h1><?php echo $buttons; ?>");
    $(".sidebar").html(records_sidebar);
    $('.btn').click(function() {
        var path = "<?php echo site_url("records/50/$category_code") ?>";
        if (String(window.location).indexOf(this.id) == -1) {
            var pool = this.id == 25 ? '50' : '25'; 
            window.location = path.replace(pool, String(this.id));
        }
    });
    
    $('#<?php echo $pool; ?>').button('toggle');
</script>

<h2><?php echo $category; ?></h2>
<table class="table table-hover">
    <thead>
        <tr>
            <th>Ala</th>
            <th>Tulemus</th>
            <th>Sportlane</th>
            <th>Kuupäev</th>
            <th>Asukoht</th>
            <th>Võistlus</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $results = array();
        foreach ($records as $row) {
            $results[] = $row->result;
        }
        $results = array_count_values($results);
        foreach ($records as &$row) {
            if ($results[$row->result] > 1) {
                $row->result = $row->result . '*';
            }
            $last_stroke = 'free';
            if (isset($row->relay)) {
                echo '<tr>';
                echo '<td>4x' . ($row->distance) / 4 . ' ' . Utils::est_stroke($row->stroke, true) . '</td>';
                echo '<td>' . $row->result . '</td>';
                $filled = false;
                foreach ($row->relay as $athlete) {
                    if ($filled)
                        echo '<tr><td colspan="2">';
                    echo '</td><td><a href="' . site_url("athletes/profile/$athlete->id") . '">' . $athlete->last_name . ', ' . $athlete->first_name . '</a></td>';
                    if (!$filled) {
                        echo '<td>' . Utils::est_date($row->date) . '</td>';
                        echo '<td>' . ($row->city == '' ? '' : $row->city . ', ') . Utils::getCountryName($row->country) . '</td>';
                        echo '<td><a href="' . site_url("competitions/summary/$row->competition_id") . '">' . $row->name . '</a></td>';
                        echo '</tr>';
                    } else {
                        echo '<td colspan="3"></td>';
                    }
                    $filled = true;
                    echo '</tr>';
                }
            } else {
                echo '<tr>';
                echo '<td>' . $row->distance . ' ' . Utils::est_stroke($row->stroke) . '</td>';
                echo '<td>' . $row->result . '</td>';
                echo '<td><a href="' . site_url("athletes/profile/$row->athlete_id") . '">' . $row->last_name . ', ' . $row->first_name . '</a></td>';
                echo '<td>' . Utils::est_date($row->date) . '</td>';
                echo '<td>' . ($row->city == '' ? '' : $row->city . ', ') . Utils::getCountryName($row->country) . '</td>';
                echo '<td><a href="' . site_url("competitions/summary/$row->competition_id") . '">' . $row->name . '</a></td>';
                echo '</tr>';
            }
        }
        ?>
    </tbody>
</table>

